/*  Neu - Feed Aggregator.
 *  Copyright (C) 2009  Joshua Montgomery
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details. 
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.neu.sql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author Joshua Montgomery
 *
 */
public class QuerySet 
{
	private ArrayList<HashMap<String,Object>> querySet = new ArrayList<HashMap<String,Object>>();
	
	private int pos = -1;
	
	private HashMap<String,Object> current;
	
	public QuerySet(ResultSet set,String columns[]) throws SQLException
	{
		int len = columns.length;
		
		while(set.next())
		{
			HashMap<String,Object> map = new HashMap<String,Object>();
			
			for(int i=0;i<len;i++)
				map.put(columns[i],set.getObject(i));			
			querySet.add(map);
		}		
	}
	
	public boolean next()
	{
		if(pos <= querySet.size())
			pos++;
		
		if(pos < querySet.size())
		{
			current = querySet.get(pos);
			return true;
		}
		else 
			current = null;
		
		return false;
	}
	
	public boolean prev()
	{
		if(pos >= -1)
			pos--;
		
		if(pos > -1)
		{
			current = querySet.get(pos);
			return true;
		}
		else
			current = null;
		
		return false;
	}
	
	public Object getValue(String column)
	{
		if(current != null)
			return current.get(column);
		return null;
	}
}
